﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MTGScore.Resources;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Shell;

namespace MTGScore
{
    public partial class HistoryPage : PhoneApplicationPage
    {
        public HistoryPage()
        {
            InitializeComponent();

            DataContext = App.ViewModel;

        }
        SavedMatches savedMatches;
        SavedMatch selectedMatch;
        bool isTrial;
        private void HistoryPage_Loaded(object sender, RoutedEventArgs e)
        {
            //DeleteHistory_Button.Text = Strings.DeleteHistory;
            //SendEmail_Button.Text = Strings.SendEmail;
            //DeleteAllHistories_Button.Text = Strings.DeleteAllHistories;


            /*
            if (App.CurrentTheme == App.Theme.Dark)
            {
                Play_Button.Source = new BitmapImage(new Uri("Images\\dark\\play.png", UriKind.Relative));
                Pause_Button.Source = new BitmapImage(new Uri("Images\\dark\\pause.png", UriKind.Relative));
                Stop_Button.Source = new BitmapImage(new Uri("Images\\dark\\stop.png", UriKind.Relative));
                Restart_Button.Source = new BitmapImage(new Uri("Images/dark\\restart.png", UriKind.Relative));
                //Play_Button.Source = "Images\dark\play.png";
            }
            else
            {

                Play_Button.Source = new BitmapImage(new Uri("Images\\light\\play.png", UriKind.Relative));
                Pause_Button.Source = new BitmapImage(new Uri("Images\\light\\pause.png", UriKind.Relative));
                Stop_Button.Source = new BitmapImage(new Uri("Images\\light\\stop.png", UriKind.Relative));
                Restart_Button.Source = new BitmapImage(new Uri("Images\\light\\restart.png", UriKind.Relative));
            }
            */
            ApplicationBarIconButton SendEmail_Button, DeleteHistory_Button;

            if (App.CurrentTheme == App.Theme.Dark)
            {
                SendEmail_Button = new ApplicationBarIconButton(new Uri("\\Images\\dark\\mail.png", UriKind.Relative)) { Text = Strings.EMailIconText, IsEnabled = true };
                DeleteHistory_Button = new ApplicationBarIconButton(new Uri("\\Images\\dark\\delete.png", UriKind.Relative)) { Text = Strings.DeleteIconText, IsEnabled = true };
            }
            else
            {
                SendEmail_Button = new ApplicationBarIconButton(new Uri("\\Images\\light\\mail.png", UriKind.Relative)) { Text = Strings.EMailIconText, IsEnabled = true };
                DeleteHistory_Button = new ApplicationBarIconButton(new Uri("\\Images\\light\\delete.png", UriKind.Relative)) { Text = Strings.DeleteIconText, IsEnabled = true };
            }
            SendEmail_Button.Click += SendEmail_Button_Click;
            DeleteHistory_Button.Click += DeleteHistory_Button_Click;

            ApplicationBar.Buttons.Add(DeleteHistory_Button);
            ApplicationBar.Buttons.Add(SendEmail_Button);
            isTrial = App.isTrial();
            if (!isTrial)
            {
                ApplicationBarMenuItem DeleteAll_Button = new ApplicationBarMenuItem(Strings.DeleteAllText);
                DeleteAll_Button.Click += DeleteAllHistories_Button_Click;
                DeleteAll_Button.IsEnabled = true;
                ApplicationBar.MenuItems.Add(DeleteAll_Button);
            }
            LoadSavedMatchesList();


            SetTrialMode();
        }
        void SetTrialMode()
        {
            if (isTrial)
            {
            }
            else
            {
                Grid.SetRowSpan(PivotControl, 2);
                AdControl.Visibility = System.Windows.Visibility.Collapsed;
            }
        }


        private void LoadSavedMatchesList()
        {
            SavedMatches_List.Items.Clear();
            savedMatches = History.LoadSavedMatches();
            for (int i = 0; i < savedMatches.matches.Count; i++)
            {
                var m = savedMatches.matches[i];
                SavedMatches_List.Items.Add(new ListBoxItem() { Content = "" + m.name, HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left, Width = 450 });
                SavedMatches_List.Items.Add(new ListBoxItem() { Content = " ", HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left, Width = 450 });
            }
            if (isTrial)
            {
                SavedMatches_List.Items.Add(new ListBoxItem() { Content = " ", HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left, Width = 450 });
                SavedMatches_List.Items.Add(new ListBoxItem() { Content = "" + Strings.BuyFullVersion, HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left, Width = 450 });
            }
            if (SavedMatches_List.Items.Count == 1)
                SavedMatches_List.SelectedItem = SavedMatches_List.Items[0];
            else
                PivotControl.SelectedItem = ListPivot_Name;
        }

        private void SavedMatches_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SavedMatches_List.SelectedItem == null)
            {
                //Moments_List.SelectedItems.Clear();
                //Moments_List.Items.Clear();
                return;
            }
            //int i = SavedMatches_List.SelectedIndex;
            //if (i >= 0 && i < savedMatches.matches.Count)
            //    selectedMatch = savedMatches.matches[i/2];
            try
            {
                selectedMatch=null;
                ListBoxItem item = (ListBoxItem)SavedMatches_List.SelectedItem;
                string s = (string)item.Content;
                if (s == " ")
                {
                    SavedMatches_List.SelectedItem=null;
                    return;
                }
                if (isTrial && s == Strings.BuyFullVersion)
                {

                    MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();

                    marketplaceDetailTask.Show();

                    SavedMatches_List.SelectedItem = null;
                    return;
                }
                for (int i = 0; i < savedMatches.matches.Count; i++)
                {
                    if (savedMatches.matches[i].name == s)
                    {
                        selectedMatch = savedMatches.matches[i];
                        break;
                    }
                }
                if (isTrial && SavedMatches_List.SelectedIndex >= 1)
                {
                    SavedMatches_List.SelectedItem = null;
                    return;
                }

                //Moments_List.SelectedItem = null;
                //History.CreateHistory(Moments_List, selectedMatch);
                App.ViewModel.SavedHistory.Clear();
                History.CreateHistory(App.ViewModel.SavedHistory, selectedMatch);

                if (selectedMatch != null)
                {
                    History_Player1_Name.Text = selectedMatch.player1_name;
                    History_Player2_Name.Text = selectedMatch.player2_name;
                    PivotControl.SelectedItem = HistoryPivot_Name;
                }
                else
                {
                    History_Player1_Name.Text = Strings.Player_1;
                    History_Player2_Name.Text = Strings.Player_2;
                    //PivotControl.SelectedItem = HistoryPivot_Name;
                    SavedMatches_List.SelectedItem=null;
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                SavedMatches_List.SelectedItem = null;
            }
        }

        private void DeleteHistory_Button_Click(object sender, EventArgs e)
        {
            if (selectedMatch != null)
            {
                if (MessageBox.Show(Strings.DeleteHistory + " " + selectedMatch.name + "?", Strings.Confirm, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    savedMatches.matches.Remove(selectedMatch);
                    selectedMatch = null;
                    History.SaveSavedMatches(savedMatches);
                    if (savedMatches.matches.Count == 0)
                        NavigationService.GoBack();
                    else
                    {
                        LoadSavedMatchesList();
                    }
                }
            }
            else if (PivotControl.SelectedItem != ListPivot_Name)
                PivotControl.SelectedItem = ListPivot_Name;
            else
                MessageBox.Show(Strings.ChooseOne);
        }

        private void SendEmail_Button_Click(object sender, EventArgs e)
        {

            if (selectedMatch != null)
            {
                EmailComposeTask etc = new EmailComposeTask();
                etc.Subject = selectedMatch.name;
                etc.Body = selectedMatch.ToString();
                etc.Show();
            }
            else if (PivotControl.SelectedItem != ListPivot_Name)
                PivotControl.SelectedItem = ListPivot_Name;
            else
                MessageBox.Show(Strings.ChooseOne);
        }

        private void DeleteAllHistories_Button_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Strings.DeleteAllHistories, Strings.Confirm, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                App.ViewModel.SavedHistory.Clear();
                selectedMatch = null;
                History.DeleteSavedMatches();
                NavigationService.GoBack();
            }
        }

        //private void Moments_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    History.SelectionChange(Moments_List);
        //}

        private void PivotControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //SavedMatches_List.SelectedItem = null;
            //Moments_List.SelectedItems.Clear();
            //Moments_List.Items.Clear();
        }
    }
}